package curso.ejercicios;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class Salvapantallas extends Activity implements OnTouchListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		View view = new RenderView(this);

		view.setOnTouchListener(this);
		setContentView(view);
	}

	class RenderView extends View {

		private Paint paint = null;
		private Random random = null;
		private int i = 0;

		public RenderView(Context context) {
			super(context);
			paint = new Paint();
			random = new Random();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawRGB(255, 255, 255);
			paint.setColor(Color.RED);
			paint.setStyle(Style.FILL);

			int width = canvas.getWidth();
			int height = canvas.getHeight();

			if (i++ == 10) {
				i = 0;
				canvas.drawRect(random.nextInt(width), random.nextInt(height),
						random.nextInt(width), random.nextInt(height), paint);
			}

			invalidate();
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			finish();
			break;
		}

		return false;
	}

}
